<!--
  * 拜访记录
  *
-->
<template>
  <!---------- 查询表单form begin ----------->
  <a-card size="small" :bordered="false" :hoverable="false" class="query-form-wrapper smart-margin-bottom10 smart-radius-2">
    <a-form>
      <a-row :gutter="[8, 10]">
        <a-col :span="5.5">
          <a-form-item label="日期" class="smart-margin-bottom0">
            <custom-range-picker v-model:value="fromData.create_time" />
          </a-form-item>
        </a-col>
        <a-col :span="3.5">
          <a-form-item label="业务员" class="smart-margin-bottom0">
            <Select name="Salesman" v-model:value="fromData.salesman_id" placeholder="业务员" @input-enter="onSearch" />
          </a-form-item>
        </a-col>
        <a-col :span="3.5">
          <a-form-item label="门店名称" class="smart-margin-bottom0">
            <a-input v-model:value.trim="fromData.store_name" placeholder="门店名称" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="3">
          <a-form-item label="门店类型" class="smart-margin-bottom0">
            <Select name="Level" v-model:value="fromData.store_level_id" placeholder="门店类型" @input-enter="onSearch" />
          </a-form-item>
        </a-col>
        <a-col :span="3">
          <a-form-item label="片区" class="smart-margin-bottom0">
            <Select name="Region" v-model:value="fromData.store_region_id" placeholder="片区" @input-enter="onSearch" />
          </a-form-item>
        </a-col>
        <a-col :span="5">
          <a-button-group>
            <a-button type="primary" html-type="submit" @click="onSearch" v-privilege="'goods:query'">
              <template #icon>
                <SearchOutlined />
              </template>
              查询
            </a-button>
            <a-button @click="resetQueryData" v-privilege="'goods:query'">
              <template #icon>
                <ReloadOutlined />
              </template>
              重置
            </a-button>
          </a-button-group>
        </a-col>
      </a-row>
    </a-form>
  </a-card>
  <!---------- 查询表单form end ----------->

  <a-card size="small" :bordered="false" :hoverable="true" class="query-table-wrapper">
    <!---------- 表格操作行 begin ----------->
    <a-row class="smart-table-btn-block">
      <div class="smart-table-operate-block"> </div>
      <div class="smart-table-setting-block">
        <TableOperator v-model="columns" :tableId="TABLE_ID_CONST.BUSINESS.ERP.GOODS" :refresh="queryData" />
      </div>
    </a-row>
    <!---------- 表格操作行 end ----------->
    <div class="table-wrapper">
      <a-table size="small" :dataSource="tableData" :columns="columns" :loading="tableLoading" rowKey="id" bordered :scroll="{ x: 'max-content', y: tableScrollY }" :pagination="false" :showSorterTooltip="false" :row-selection="{ selectedRowKeys: selectedRowKeyList, onChange: onSelectChange }">
        <template #bodyCell="{ text, record, column }">
          <template v-if="column.dataIndex === 'store_name'">
            <a @click="detail(record)">{{ record?.store?.store_name }}</a>
          </template>
          <template v-if="column.dataIndex === 'purchase_status'">
            <a-tag :color="TagColor[text]">{{ getDescByValue('APPROVAL_STA_ENUM', Number(text)) }}</a-tag>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <div class="smart-table-operate">
              <a-button @click="detail(record)" type="link" style="color: #97999c" v-privilege="'goods:update'">详情</a-button>
            </div>
          </template>
        </template>
        <template #summary>
          <a-table-summary-row>
            <a-table-summary-cell>总计</a-table-summary-cell>
            <a-table-summary-cell />
            <a-table-summary-cell />
            <a-table-summary-cell>
              <a-typography-text type="danger">{{ total_amount.total_visit_duration_sum.toFixed(2) }}</a-typography-text>
            </a-table-summary-cell>
            <a-table-summary-cell />
            <a-table-summary-cell />
            <a-table-summary-cell />
            <a-table-summary-cell />
            <a-table-summary-cell />
            <a-table-summary-cell />
            <a-table-summary-cell />
          </a-table-summary-row>
        </template>
      </a-table>
    </div>
    <div class="smart-query-table-page query-table-page">
      <a-pagination showSizeChanger showQuickJumper show-less-items :pageSizeOptions="PAGE_SIZE_OPTIONS" :defaultPageSize="queryForm.limit" v-model:current="queryForm.page" v-model:pageSize="queryForm.limit" :total="total" @change="queryData" @showSizeChange="queryData" :show-total="(total) => `共${total}条`" />
    </div>
  </a-card>
  <!---------- 详情 begin ----------->
  <RecordsDetailsModal ref="detailRef" />
  <!---------- 详情 end ----------->
</template>
<script setup lang="ts">
import CustomRangePicker from '@/components/business/custom-range-picker/index.vue'
import RecordsDetailsModal from './components/records-details-modal.vue'
import Select from '@/components/business/select/index.vue'
import { visitApi } from '@/api/business/marketing/visit-api'
import { PAGE_SIZE_OPTIONS, DEFAULT_TIME_RANGE } from '@/constants/system/common-const'
import TableOperator from '@/components/support/table-operator/index.vue'
import { TABLE_ID_CONST } from '@/constants/support/table-id-const'
import { useQuery } from '@/hooks/web/use-query-hook'
import { useEnums } from '@/hooks/plugins/useEnums'
import { useAppConfigStore } from '@/store/modules/app-store'
import { useResize } from '@/hooks/web/use-resize-hook'
import { useRoute } from 'vue-router'
import { ref, onMounted } from 'vue'

const route = useRoute()
const appConfigStore = useAppConfigStore()
const { getDescByValue } = useEnums()
// ----------------------------- tag单据状态 -----------------------------
enum TagColor {
  'processing' = 1,
  'success',
  'orange',
  'error',
  'warning'
}

// ----------------------------- 计算表格高度 -----------------------------
const { tableScrollY } = useResize()

// ---------------------------- 表格列 ----------------------------
const columns = computed(() => [
  {
    title: '门店',
    dataIndex: 'store_name',
    width: appConfigStore.$state.compactFlag ? 170 : 210
  },
  {
    title: '门店类型',
    dataIndex: ['store', 'level', 'level_name']
  },
  {
    title: '拜访时长(分钟)',
    dataIndex: 'visit_duration'
  },
  {
    title: '拜访开始时间',
    dataIndex: 'sign_in_time'
  },
  {
    title: '拜访结束时间',
    dataIndex: 'sign_out_time'
  },
  {
    title: '陈列数量',
    dataIndex: ''
  },
  {
    title: '业务员',
    dataIndex: ['salesman', 'rename']
  },
  {
    title: '所属线路',
    dataIndex: ['visitline', 'name']
  },
  {
    title: '所属片区',
    dataIndex: ['store', 'region', 'name']
  },
  {
    title: '操作',
    dataIndex: 'action',
    fixed: 'right',
    width: appConfigStore.$state.compactFlag ? 80 : 100
  }
])

// ---------------------------- 查询数据表单和方法 ----------------------------
const fromData = ref({
  store_name: '',
  store_level_id: undefined,
  store_region_id: undefined,
  create_time: DEFAULT_TIME_RANGE.value,
  salesman_id: undefined
})
const total_amount = ref({
  total_visit_duration_sum: 0
})
const { queryForm, tableData, tableLoading, total, resetQuery, onSearch, queryData, deleteData } = useQuery({
  fetchDataApi: async (params) => {
    const param = {
      ...params,
      store_name: fromData.value.store_name,
      store_level_id: fromData.value.store_level_id,
      store_region_id: fromData.value.store_region_id,
      create_time: fromData.value.create_time,
      salesman_id: fromData.value.salesman_id
    }
    const res = await visitApi.queryVisitList(param)
    const { data, ...other } = res.data
    total_amount.value = {
      ...other
    }
    return {
      list: res.data.data,
      total: res.data.total
    }
  },
  queryFormFilter: {},
  queryFormOp: {}
})

onActivated(queryData)

onMounted(() => {
  fromData.value.salesman_id = Number(route.query.salesman_id) || undefined
  fromData.value.create_time = route.query.create_time || DEFAULT_TIME_RANGE.value
})

const resetQueryData = () => {
  fromData.value.store_name = ''
  fromData.value.store_level_id = undefined
  fromData.value.store_region_id = undefined
  fromData.value.create_time = DEFAULT_TIME_RANGE.value
  fromData.value.salesman_id = undefined
  resetQuery()
}

// ---------------------------- 详情 ----------------------------
const detailRef = ref()

const detail = (rowData) => {
  detailRef.value.showDrawer(rowData)
}

// 选择表格行
const selectedRowKeyList = ref([])

function onSelectChange(selectedRowKeys) {
  selectedRowKeyList.value = selectedRowKeys
}
</script>
